﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Input;

namespace Steamrunner
{
    class GameOverScreen : Screen
    {
        Texture2D background = null;
        Texture2D title = null;

        Texture2D first = null;
        Texture2D second = null;
        Texture2D third = null;
        Texture2D fourth = null;

        Texture2D blue1 = null;
        Texture2D blue2 = null;
        Texture2D blue3 = null;
        Texture2D blue4 = null;

        Texture2D yellow1 = null;
        Texture2D yellow2 = null;
        Texture2D yellow3 = null;
        Texture2D yellow4 = null;

        Texture2D red1 = null;
        Texture2D red2 = null;
        Texture2D red3 = null;
        Texture2D red4 = null;

        Texture2D green1 = null;
        Texture2D green2 = null;
        Texture2D green3 = null;
        Texture2D green4 = null;

        SpriteFont font = null;

        Score score;

        int[] firstTime;
        int[] secondTime;
        int[] thirdTime;
        int[] fourthTime;

        bool justInitialized = false;

        string nextScreen;

        public Rectangle screenBoundaries;

        public GameOverScreen(ScreenManager screenManager, Game game)
            : base(screenManager, game)
        {
        }

        public override void Initialize()
        {
            musicManager.PlayMusic("mainMenuTheme");

            justInitialized = true;

            base.Initialize();
        }

        protected override void LoadContent()
        {
            screenBoundaries = Game.Window.ClientBounds;

            font = Game.Content.Load<SpriteFont>(@"Fonts\timeFont");

            background = Game.Content.Load<Texture2D>(@"Images\Menus\background");
            title = Game.Content.Load<Texture2D>(@"Images\Menus\go_title");

            guiManager = new GuiManager(Game);

            Button button = new Button(Game, Spritebatch, "Images\\Menus\\go_btmm", new Vector2(screenBoundaries.Width * 0.245f, screenBoundaries.Height * 0.825f), "MenuScreen");
            guiManager.AddButton(button);

            guiManager.Initialize();

            score = new Score();

            blue1 = Game.Content.Load<Texture2D>(@"Images\Ingame\blue_1st");
            blue2 = Game.Content.Load<Texture2D>(@"Images\Ingame\blue_2nd");
            blue3 = Game.Content.Load<Texture2D>(@"Images\Ingame\blue_3rd");
            blue4 = Game.Content.Load<Texture2D>(@"Images\Ingame\blue_4th");

            yellow1 = Game.Content.Load<Texture2D>(@"Images\Ingame\yellow_1st");
            yellow2 = Game.Content.Load<Texture2D>(@"Images\Ingame\yellow_2nd");
            yellow3 = Game.Content.Load<Texture2D>(@"Images\Ingame\yellow_3rd");
            yellow4 = Game.Content.Load<Texture2D>(@"Images\Ingame\yellow_4th");

            red1 = Game.Content.Load<Texture2D>(@"Images\Ingame\red_1st");
            red2 = Game.Content.Load<Texture2D>(@"Images\Ingame\red_2nd");
            red3 = Game.Content.Load<Texture2D>(@"Images\Ingame\red_3rd");
            red4 = Game.Content.Load<Texture2D>(@"Images\Ingame\red_4th");

            green1 = Game.Content.Load<Texture2D>(@"Images\Ingame\green_1st");
            green2 = Game.Content.Load<Texture2D>(@"Images\Ingame\green_2nd");
            green3 = Game.Content.Load<Texture2D>(@"Images\Ingame\green_3rd");
            green4 = Game.Content.Load<Texture2D>(@"Images\Ingame\green_4th");
        }

        public override void Update(GameTime gameTime)
        {
            if (justInitialized)
            {
                List<short> places = score.PlayerPlaces;

                if (places[0] == 1)
                {
                    first = blue1;
                    firstTime = score.PlayerTimes[0];
                }
                else if (places[0] == 2)
                {
                    second = blue2;
                    secondTime = score.PlayerTimes[0];
                }
                else if (places[0] == 3)
                {
                    third = blue3;
                    thirdTime = score.PlayerTimes[0];
                }
                else if (places[0] == 4)
                {
                    fourth = blue4;
                    fourthTime = score.PlayerTimes[0];
                }

                if (score.AmountOfPlayers > 1)
                {
                    if (places[1] == 1)
                    {
                        first = red1;
                        firstTime = score.PlayerTimes[1];
                    }
                    else if (places[1] == 2)
                    {
                        second = red2;
                        secondTime = score.PlayerTimes[1];
                    }
                    else if (places[1] == 3)
                    {
                        third = red3;
                        thirdTime = score.PlayerTimes[1];
                    }
                    else if (places[1] == 4)
                    {
                        fourth = red4;
                        fourthTime = score.PlayerTimes[1];
                    }
                }

                if (score.AmountOfPlayers > 2)
                {
                    if (places[2] == 1)
                    {
                        first = yellow1;
                        firstTime = score.PlayerTimes[2];
                    }
                    else if (places[2] == 2)
                    {
                        second = yellow2;
                        secondTime = score.PlayerTimes[2];
                    }
                    else if (places[2] == 3)
                    {
                        third = yellow3;
                        thirdTime = score.PlayerTimes[2];
                    }
                    else if (places[2] == 4)
                    {
                        fourth = yellow4;
                        fourthTime = score.PlayerTimes[2];
                    }
                }

                if (score.AmountOfPlayers > 3)
                {
                    if (places[3] == 1)
                    {
                        first = green1;
                        firstTime = score.PlayerTimes[3];
                    }
                    else if (places[3] == 2)
                    {
                        second = green2;
                        secondTime = score.PlayerTimes[3];
                    }
                    else if (places[3] == 3)
                    {
                        third = green3;
                        thirdTime = score.PlayerTimes[3];
                    }
                    else if (places[3] == 4)
                    {
                        fourth = green4;
                        fourthTime = score.PlayerTimes[3];
                    }
                }

                justInitialized = false;
            }

            musicManager.Update(gameTime);

            inputManager.Update(gameTime);

            if (inputManager.JustPressed(Keys.W))
            {
                guiManager.JoystickUp();
                musicManager.PlaySound("buttonScrolling");
            }
            else if (inputManager.JustPressed(Keys.S))
            {
                guiManager.JoystickDown();
                musicManager.PlaySound("buttonScrolling");
            }

            if (inputManager.JustPressed(Keys.A) || inputManager.JustPressed(Keys.D))
            {
                nextScreen = guiManager.ButtonClicked();
                musicManager.PlaySound("buttonSelect");
                screenManager.CurrentScreen = nextScreen;
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            Spritebatch.Begin();

            Spritebatch.Draw(background, Vector2.Zero, Color.White);

            Spritebatch.Draw(title, Vector2.Zero, Color.White);

            if (score.AmountOfPlayers > 2 && third != null)
            {
                Spritebatch.Draw(third, new Vector2(screenBoundaries.Width * 0.25f, screenBoundaries.Height * 0.481f), Color.White);
                string temp = thirdTime[0].ToString() + ":";
                if (thirdTime[1] < 10)
                {
                    temp += "0";
                }
                temp += thirdTime[1].ToString() + ":";
                if (thirdTime[2] < 10)
                {
                    temp += "00";
                }
                else if (thirdTime[2] < 100)
                {
                    temp += "0";
                }
                temp += thirdTime[2];
                Spritebatch.DrawString(font, temp, new Vector2(screenBoundaries.Width * 0.25f + third.Width - 50f, screenBoundaries.Height * 0.411f + 150f), Color.Black);
            }
            if (score.AmountOfPlayers > 1 && second != null)
            {
                Spritebatch.Draw(second, new Vector2(screenBoundaries.Width * 0.085f, screenBoundaries.Height * 0.436f), Color.White);
                string temp = secondTime[0].ToString() + ":";
                if (secondTime[1] < 10)
                {
                    temp += "0";
                }
                temp += secondTime[1].ToString() + ":";
                if (secondTime[2] < 10)
                {
                    temp += "00";
                }
                else if (secondTime[2] < 100)
                {
                    temp += "0";
                }
                temp += secondTime[2];
                Spritebatch.DrawString(font, temp, new Vector2(screenBoundaries.Width * 0.085f + (second.Width * 0.5f) - (font.MeasureString(temp).X * 0.5f) - 40f, screenBoundaries.Height * 0.366f), Color.Black);
            }
            if (first != null)
            {
                Spritebatch.Draw(first, new Vector2(screenBoundaries.Width * 0.186f, screenBoundaries.Height * 0.35f), Color.White);
                string temp = firstTime[0].ToString() + ":";
                if (firstTime[1] < 10)
                {
                    temp += "0";
                }
                temp += firstTime[1].ToString() + ":";
                if (firstTime[2] < 10)
                {
                    temp += "00";
                }
                else if (firstTime[2] < 100)
                {
                    temp += "0";
                }
                temp += firstTime[2];
                Spritebatch.DrawString(font, temp, new Vector2(screenBoundaries.Width * 0.186f + (first.Width * 0.5f) - (font.MeasureString(temp).X * 0.5f), screenBoundaries.Height * 0.28f), Color.Black);
            }
            if (score.AmountOfPlayers == 4 && fourth != null)
            {
                Spritebatch.Draw(fourth, new Vector2(screenBoundaries.Width * 0.74f, screenBoundaries.Height * 0.384f), Color.White);
                string temp = fourthTime[0].ToString() + ":";
                if (fourthTime[1] < 10)
                {
                    temp += "0";
                }
                temp += fourthTime[1].ToString() + ":";
                if (fourthTime[2] < 10)
                {
                    temp += "00";
                }
                else if (fourthTime[2] < 100)
                {
                    temp += "0";
                }
                temp += fourthTime[2];
                Spritebatch.DrawString(font, temp, new Vector2(screenBoundaries.Width * 0.74f + (fourth.Width * 0.5f) - (font.MeasureString(temp).X * 0.5f), screenBoundaries.Height * 0.314f), Color.Black);
            }

            guiManager.Draw(gameTime);

            Spritebatch.End();

            base.Draw(gameTime);
        }

        public override string GetNextScreen()
        {
            return nextScreen;
        }
    }
}
